<template>
  <div class="page-box">
    <universal-table :columns="columns" :fetch-data="fetchTableData" permission-prefix="order:store"
      :operationWidth="'300'" :show-selection="false" :show-index="false" :bt1Name="'查看'" :export-file-name="'售后订单'"
      :init-search-form="searchForm" @edit="handleEdit" @delete="handleDelete" @reset="reset"
      @status-change="handleStatusChange" ref="table">

      <!-- 操作扩展 -->
      <template slot="operationExtend" slot-scope="{ row }">
        <el-button size="mini" type="warning" :row=row @click="handleActionApprove(row)"
          v-if="permission.includes('order:store:edit') && row.status==1">审核
        </el-button>
      </template>

      <template slot="operationExtend2" slot-scope="{ row }">
        <el-button size="mini" type="warning" :row=row @click="handleActionRefundTip(row)"
          v-if="permission.includes('order:store:edit') && row.status==2 && row.refund_express">退款
        </el-button>
      </template>


      <!-- 搜索区域扩展 -->
      <template #searchExtend>
        <el-form-item prop="status">
          <el-select v-model="searchForm.status" placeholder="售后状态" clearable>
            <el-option v-for="item in afterSalesOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </template>

    </universal-table>

    <!-- 详情弹窗 -->
    <el-dialog title="售后详情" :visible.sync="dialogDeliveryVisible" width="60%" :close-on-click-modal="autoClick">
      <orderAfterSales v-if="currentRow" :formData="currentRow" @afterSales="afterSalesOrder" />
    </el-dialog>



  </div>
</template>

<script>
  import {
    mapGetters
  } from "vuex";
  import UniversalTable from "@/components/TableComponent.vue";
  import orderAfterSales from "./orderAfterSales.vue";




  import {
    STORE_ORDER_AFTER_SALES_STATUS
  } from '@/constants/statusMap';
  export default {
    components: {
      UniversalTable,
      orderAfterSales


    },
    data() {
      return {
        STORE_ORDER_AFTER_SALES_STATUS,
        dialogAfterSalesVisible: false,
        dialogDeliveryVisible: false,
        dialogVisible: false,
        currentRow: null,
        autoClick: false,
        columns: [{
            prop: "id",
            label: "ID"
          },
          {
            prop: "order_no",
            label: "订单号"
          },
          {
            prop: "after_sales_no",
            label: "售后单号"
          },
          {
            prop: "merchant_name",
            label: "商户名称"
          },
          {
            prop: "phone_number",
            label: "用户电话"
          },
          {
            prop: "store_name",
            label: "商品名称"
          },

          {
            prop: "spec_values",
            label: "商品规格"
          },
          {
            prop: "oi_price",
            label: "购买价格"
          },
          {
            prop: "cart_num",
            label: "购买数量"
          },
          {
            prop: "oi_total_price",
            label: "购买总价"
          },
          {
            prop: "create_time",
            label: "创建时间"
          },
          {
            prop: "aft_status",
            label: "售后状态",
            tag: true,
            tagConfig: STORE_ORDER_AFTER_SALES_STATUS
          },

        ],

        afterSalesOptions: [{
            id: 999,
            name: "售后状态"
          },
          {
            id: 1,
            name: "待审核"
          },
          {
            id: 2,
            name: "通过"
          },
          {
            id: 3,
            name: "拒绝"
          },
          {
            id: 4,
            name: "取消"
          },
          {
            id: 5,
            name: "完成"
          },
        ],


        searchForm: {
          afterSalesOptions: 999,
          status: 999,


        },
      };
    },
    computed: {
      ...mapGetters(["permission"]),
    },
    methods: {
      afterSalesOrder(body) {
        console.log('---handleAfterSales-body--', body)
        this.$httpRequest.post('/AfterSales/hand', body).then(result => {
          console.log('--afterSalesOrder--result------', result)
          if (result.code == 0) {
            this.$message.success('售后处理成功')
            this.$refs.table.refresh()
            this.dialogDeliveryVisible = false;

          } else {
            this.$message.error('售后处理失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });
      },
      handleActionRefundTip(row) {
        this.$confirm('此操作将退款给用户，退款后不可返回，确定吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleActionRefund(row);

        }).catch(() => {
          // 用户点击取消
          this.$message({
            type: 'info',
            message: '已取消退款'
          });
        });
      },

      //退款
      handleActionRefund(row) {
        this.$httpRequest.post('/AfterSales/refund', {
          id: row.id
        }).then(result => {
          console.log('--handleActionRefund-----', result)
          if (result.code == 0) {
            this.$message.success('退款成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('退款失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });

      },
      handleActionApprove(row) {
        this.currentRow = {
          ...row
        };
        this.dialogDeliveryVisible = true;
      },
      // 获取表格数据
      async fetchTableData(params = {}) {
        const postData = Object.assign({}, params, this.searchForm);
        try {
          const result = await this.$httpRequest.post('/AfterSales/index', postData)
          console.log('-------fetchTableData----', result)
          return result
        } catch (e) {
          this.$message.error(e.message)
          throw e // 保持错误继续向上传递
        }

      },
      //重置
      reset() {
        this.searchForm.status = 999;
        this.searchForm.afterSalesStatus = 999;
      },
      // 编辑
      handleEdit(row) {
        // 打开编辑对话框逻辑
        this.currentRow = {
          ...row
        }; // 可深拷贝
        this.dialogDeliveryVisible = true;
      },

      // 删除
      async handleDelete(row) {
        console.log("删除:", row);
      },

      // 状态变更
      async handleStatusChange(row) {
        console.log("状态变更:", row);
        this.$message.success("状态更新成功");
      },
      shipingOrder(data) {
        console.log("----收到物流数据:", data);
        this.$httpRequest.post('/order/shiping', data).then(result => {
          console.log('--approved---result------', result)
          if (result.code == 0) {
            this.$message.success('配送成功')
            this.$refs.table.refresh()
            this.dialogDeliveryVisible = false;

          } else {
            this.$message.error('配送失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });





      },
      //取消订单
      handleApprove(payload) {
        console.log("信息：", payload);

        this.$httpRequest.post('/order/cancel', payload).then(result => {
          console.log('--approved---result------', result)
          if (result.code == 0) {
            this.$message.success('取消成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('取消失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });
        this.dialogVisible = false;
      },
    },
  };
</script>

<style>
  .page-box {
    padding: 20px;

  }
</style>
